﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Util;
using System.IO;
using log4net;
using System.Reflection;

namespace DAL
{
    public class CsvDb
    {
        private static ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        public String OutputPath;

        private bool firstTime = true;

        private StreamWriter writer = null;

        private bool EnableRepeation;//是否开启判重

        private HashSet<IMG> hsImgs;

        private String imgDir;

        private void InitalRepeation()
        {
            String[] arr = OutputPath.Split('\\');
            String dir = String.Format("repeation\\{0}", arr[2]);
            String[] arr_1 = Directory.GetFiles(dir);
            if (arr_1.Count() > 0)
            {
                String filename = arr_1[0];
                Product p_r = XmlUtil.XmlDeserialize(typeof(Product), filename) as Product;
                hsImgs = new HashSet<IMG>(p_r.IMGs);
            }
        }

        public CsvDb(String outputpath, bool enbaleRepeation)
        {
            this.EnableRepeation = enbaleRepeation;
            this.OutputPath = outputpath;
            String csvName = OutputPath + "_csv.csv";
            writer = new StreamWriter(csvName, true, Encoding.GetEncoding("GBK"));

            imgDir = outputpath + "_img";
            if (!Directory.Exists(imgDir))
            {
                Directory.CreateDirectory(imgDir);
            }

            InitalRepeation();
        }

        public bool Add(Entity entity)
        {
            if (firstTime)
            {
                String[] arr = entity.xmlFile.Split('\\');
                String target = "repeation\\" + arr[2] + "\\" + arr[4];
                File.Copy(entity.xmlFile, target, true);
                firstTime = false;
            }

            Product p = XmlUtil.XmlDeserialize(typeof(Product), entity.xmlFile) as Product;

            if (hsImgs != null && EnableRepeation)
            {
                if (Repeat(p))
                {
                    log.Info("data repeat ,return");
                    return true;
                }
            }
            String relative = imgDir.Split('\\').ToList().Last();
            List<String> lines = p.ToCsv(entity.imgDir,imgDir,relative);
            lines.ForEach(line => writer.WriteLine(line));
            writer.Flush();

            return false;
        }

        private bool Repeat(Product product)
        {
            foreach (IMG img in product.IMGs)
            {
                if (hsImgs.Contains(img))
                {
                    return true;//repeation
                }
            }
            return false;
        }


        public void Close()
        {
            if (writer != null)
            {
                writer.Close();
                writer.Dispose();
            }
        }
    }
}
